package com.jt.exception;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 金帮嘉诚 @date 2021-08-20 星期五 16:54
 * 自定义降级时的异常处理
 */

@Component//交给Spring管理
//@ConditionalOnMissingBean(BlockExceptionHandler.class)//底层的错误处理器使用了该注解 使默认的处理方式会被程序员自己构建的替代
public class SentinelHandler implements BlockExceptionHandler {

    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       BlockException e){
        // Return 429 (Too Many Requests) by default.
//        response.setStatus(429);

        //1.设置响应数据编码
//            response.setCharacterEncoding("utf-8");
        //2.告诉浏览器 要输出的类型编码
//            response.setContentType("text/html;charset=utf-8");
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

//            out.println("??????遇见了某个错误?????");
//            Map<String, Object> map = new HashMap<>();
//            if (e instanceof DegradeException) {
//                map.put("statue", "601");
//                map.put("message", "服务被熔断了");
//            } else if (e instanceof FlowException) {
//                map.put("statue", "602");
//                map.put("message", "服务被限流了");
//            } else {
//                map.put("statue", "603");
//                map.put("message", "服务被sentinel限制了");
//            }
//            String jsonString = new ObjectMapper().writeValueAsString(map);
        //            out.println(jsonString);
        System.out.println("e.getClass() = " + e.getClass());
        out.println(e.getClass());

        String msg = "";
        if (e instanceof FlowException) {
            msg = "限流了";
        } else if (e instanceof DegradeException) {
            msg = "降级了";
        } else if (e instanceof ParamFlowException) {
            msg = "热点参数限流";
        } else if (e instanceof SystemBlockException) {
            msg = "系统规则（负载/...不满足要求）";
        } else if (e instanceof AuthorityException) {
            msg = "授权规则不通过";
        }else {
            msg = "其他异常";
        }
        out.println(msg);

        out.flush();
        out.close();

    }
}
